package com.nidu.demo.dept.model;

import com.alibaba.cola.exception.BizException;
import com.nidu.demo.base.BaseDeletableEntity;
import com.nidu.demo.base.BaseStatusEntity;
import com.nidu.demo.base.BaseTenantEntity;
import com.nidu.demo.common.enums.BooleanEnum;
import com.nidu.demo.common.enums.StatusEnum;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * 部门领域对象
 *
 * @author auto
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
public class Department extends BaseTenantEntity implements BaseStatusEntity, BaseDeletableEntity {

    private Long id;

    /**
     * 部门名称
     */
    private String deptName;

    /**
     * 部门编码
     */
    private String deptCode;

    /**
     * 父部门ID
     */
    private Long parentId;

    /**
     * 组级列表
     */
    private String ancestors;

    /**
     * 部门领导人ID
     */
    private Long leader;

    /**
     * 部门联系电话
     */
    private String phoneNumber;

    /**
     * 部门邮件
     */
    private String email;

    /**
     * 公司ID
     */
    private Long companyId;

    /**
     * 描述
     */
    private String remark;

    /**
     * 排序值
     */
    private Integer sortOrder;

    /**
     * 状态
     */
    private Integer status;

    /**
     * 是否删除
     */
    private Boolean deleted;

    /**
     * 创建部门
     * @param name 部门名称
     * @param code 部门编码
     * @param parentId 父部门ID
     * @param companyId 公司ID
     * @param description 描述
     * @param sortOrder 排序值
     * @param leader 部门领导人ID
     * @param phoneNumber 部门联系电话
     * @param email 部门邮件
     * @return 部门对象
     */
    public static Department create(String name, String code, Long parentId, Long companyId, String remark,
                                   Integer sortOrder, Long leader, String phoneNumber, String email) {
        Department dept = new Department();
        dept.setDeptName(name)
                .setDeptCode(code)
                .setParentId(parentId)
                .setCompanyId(companyId)
                .setRemark(remark)
                .setSortOrder(sortOrder)
                .setLeader(leader)
                .setPhoneNumber(phoneNumber)
                .setEmail(email);
        return dept;
    }

    public static Department create(String name, String code, Long parentId, Long companyId, String phoneNumber, String remark) {
        Department dept = new Department();
        dept.setDeptName(name)
                .setDeptCode(code)
                .setParentId(parentId)
                .setCompanyId(companyId)
                .setPhoneNumber(phoneNumber)
                .setRemark(remark)
                .setSortOrder(0);
        return dept;
    }

    /**
     * 更新部门
     * @param id 部门ID
     * @param name 部门名称
     * @param code 部门编码
     * @param parentId 父部门ID
     * @param companyId 公司ID
     * @param description 描述
     * @param sortOrder 排序值
     * @param leader 部门领导人ID
     * @param phoneNumber 部门联系电话
     * @param email 部门邮件
     * @return 部门对象
     */
    public static Department update(Long id, String name, String code, Long parentId, Long companyId, String remark,
                                   Integer sortOrder, Long leader, String phoneNumber, String email) {
        Department dept = new Department();
        dept.setId(id)
                .setDeptName(name)
                .setDeptCode(code)
                .setParentId(parentId)
                .setCompanyId(companyId)
                .setRemark(remark)
                .setSortOrder(sortOrder)
                .setLeader(leader)
                .setPhoneNumber(phoneNumber)
                .setEmail(email);
        return dept;
    }

    @Override
    public void isDeleted() {
        if (BooleanEnum.TRUE.getValue().equals(this.deleted)) {
            throw new BizException(ErrorCodeConstants.DEPARTMENT_NOT_FOUND.getCode(), ErrorCodeConstants.DEPARTMENT_NOT_FOUND.getMessage());
        }
    }

    @Override
    public void isDisable() {
        if (StatusEnum.DISABLE.getValue().equals(this.status)) {
            throw new BizException(ErrorCodeConstants.DEPARTMENT_STATUS_DISABLE.getCode(), ErrorCodeConstants.DEPARTMENT_STATUS_DISABLE.getMessage());
        }
    }
}